import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RedisDemoHash {
    public static void test1(Jedis jedis) {
        System.out.println("hset 和 hget :");
        jedis.flushAll();
        jedis.hset("key","f1","111");
        Map<String,String> fields = new HashMap<String, String>();
        fields.put("f2","222");
        fields.put("f3","333");
        jedis.hset("key",fields);
        String result =jedis.hget("key","f1");
        System.out.println(result);
        result = jedis.hget("key","f2");
        System.out.println(result);
        result = jedis.hget("key","f100");
        System.out.println(result);

    }
    public static void test2(Jedis jedis) {
        System.out.println("hexists :");
        jedis.flushAll();
        jedis.hset("key","f1","111");
        jedis.hset("key","f2","222");
        jedis.hset("key","f3","333");
        boolean result = jedis.hexists("key","f1");
        System.out.println(result);
        result = jedis.hexists("key","f100");
        System.out.println(result);
    }
    public static void test3(Jedis jedis) {
        System.out.println("hdel :");
        jedis.flushAll();
        jedis.hset("key","f1","111");
        jedis.hset("key","f2","222");
        jedis.hset("key","f3","333");
        long count = jedis.hdel("key","f1","f2");
        System.out.println(count);
        boolean result = jedis.hexists("key","f1");
        System.out.println(result);
        result = jedis.hexists("key","f3");
        System.out.println(result);
    }
    public static void test4(Jedis jedis) {
        System.out.println("hkeys 和 hvals");
        jedis.flushAll();

        jedis.hset("key","f1","111");
        jedis.hset("key","f2","222");
        jedis.hset("key","f3","333");
        Set<String> keys = jedis.hkeys("key");
        System.out.println(keys);
        List<String> values = jedis.hvals("key");
        System.out.println(values);
    }
    public static void test5(Jedis jedis) {
        System.out.println("hmget 和 hmset :");
        jedis.flushAll();
        Map<String,String> map = new HashMap<>();
        map.put("f1","111");
        map.put("f2","222");
        map.put("f3","333");
        jedis.hmset("key",map);
        List<String> values = jedis.hmget("key","f1","f2","f3");
        System.out.println(values);
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("127.0.0.1", 8888);
        try (Jedis jedis = jedisPool.getResource()) {
            test5(jedis);
        }
    }
}
